package com.cy.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cy.system.annotation.SysLog;
import com.cy.system.dto.RoleDto;
import com.cy.system.entity.Role;
import com.cy.system.entity.RoleDept;
import com.cy.system.entity.RoleMenu;
import com.cy.system.entity.RoleOper;
import com.cy.system.entity.User;
import com.cy.system.mapper.RoleMapper;
import com.cy.system.service.RoleService;
import com.github.pagehelper.PageHelper;

@Service
public class RoleServiceImpl implements RoleService {
	
	private final static Logger logger = Logger.getLogger(RoleServiceImpl.class);
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
	
	@Resource
	RoleMapper roleMapper;
	
	@Override
	@SysLog(operType="编辑角色")
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
	public int editRole(RoleDto roleDto,HttpSession session) {	
		logger.info("==> 进入方法 editRole roleDto["+roleDto+"]");
		Role role = new Role();
		if(roleDto.getRole().getRoleId()==0) {
			role.setRoleName(roleDto.getRole().getRoleName());
			this.addRole(role,session);
			if(0 != role.getRoleId()) {
				List<RoleMenu> roleMenuList = roleDto.getRoleMenus();
				for(RoleMenu rm : roleMenuList) {
					rm.setRoleId(role.getRoleId());
				}
				this.addRoleMenusBatch(roleMenuList);
				logger.info("==> editRole roleMenuList["+roleMenuList+"]");	
				return 1;
			} 					
		}else {
			role = roleDto.getRole();
			logger.info("==> 更新role["+role+"]");
			this.updateRole(role,session);
			this.delRoleMenusById(role.getRoleId());
			this.addRoleMenusBatch(roleDto.getRoleMenus());
			logger.info("==> 更新roleMenus["+roleDto.getRoleMenus()+"]");		
			return 1;
		}
		return 1;
	}

	@Override
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
	public int delRoleById(int roleId) {
		// TODO Auto-generated method stub
		return roleMapper.delRoleById(roleId);
	}

	@Override
	@SysLog(operType="更新角色",isWrite=false)
	public int updateRole(Role role,HttpSession session) {
		User user = (User) session.getAttribute("userSession");
		role.setUpdateBy(user.getUserName());
		role.setUpdateDate(sdf.format(new Date()));
		return roleMapper.updateRole(role);
	}

	@Override
	public List<Role> getAllRole(int pageNum,int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		List<Role> resList = roleMapper.getAllRole();
		return resList;
	}
	
	@Override
	public List<Role> getAllRole() {
		List<Role> resList = roleMapper.getAllRole();
		return resList;
	}

	@Override
	public Role getRoleById(int roleId) {
		return roleMapper.getRoleById(roleId);
	}

	@Override
	public List<Integer> getMenuIdsByRoleId(int roleId) {
		return roleMapper.getMenuIdsByRoleId(roleId);
	}
	
	@Override
	public List<Integer> getDeptIdsByRoleId(int roleId) {		
		return roleMapper.getDeptIdsByRoleId(roleId);
	}

	@Override
	public List<Integer> getOperIdsByRoleId(int roleId) {
		return roleMapper.getOperIdsByRoleId(roleId);
	}

	@Override
	public int addRole(Role role,HttpSession session) {	
		User user = (User) session.getAttribute("userSession");
		role.setUpdateBy(user.getUserName());
		role.setUpdateDate(sdf.format(new Date()));
		logger.info("==> addRole "+role);
		return roleMapper.addRole(role);
	}

	@Override
	@SysLog(operType="批量插入RoleMenu",isWrite=false)
	public int addRoleMenusBatch(List<RoleMenu> list) {
		if(null == list || list.size() == 0) {
			return 1;
		}
		return roleMapper.addRoleMenusBatch(list);
	}
	
	@Override
	@SysLog(operType="批量删除RoleMenu",isWrite=false)
	public int delRoleMenusById(int roleId) {
		return roleMapper.delRoleMenusById(roleId);
	}
	
	@Override
	@SysLog(operType="批量插入RoleMenu",isWrite=false)
	public int addRoleDeptsBatch(List<RoleDept> list) {
		if(null == list || list.size() == 0) {
			return 1;
		}
		return roleMapper.addRoleDeptsBatch(list);
	}
	
	@Override
	@SysLog(operType="批量删除RoleMenu",isWrite=false)
	public int delRoleDeptsById(int roleId) {
		return roleMapper.delRoleDeptsById(roleId);
	}
	
	@Override
	@SysLog(operType="批量插入RoleMenu",isWrite=false)
	public int addRoleOpersBatch(List<RoleOper> list) {
		if(null == list || list.size() == 0) {
			return 1;
		}
		return roleMapper.addRoleOpersBatch(list);
	}
	
	@Override
	@SysLog(operType="批量删除RoleMenu",isWrite=false)
	public int delRoleOpersById(int roleId) {
		return roleMapper.delRoleOpersById(roleId);
	}
	
}
